$(document).keyup(function(event){
 switch(event.keyCode) {
 	case 13:{
		queryBtn_Filter();//重新查询数据
 		/*var isHidden_win = $("#win").parent().is(":hidden");
 		var isHidden_win_common = $("#win_common").parent().is(":hidden");
 		
 		if(isHidden_win == true && isHidden_win_common==true){
	        queryBtn_Filter();//重新查询数据
	        break;
	 	}
 		//提交form
 		if(isHidden_win == false){
 			formAddSubmit();
 			break;
 		}*/
	 	break;
     }
	 case 27:
	 case 96:{
		$('#win').window('close');
		$('#win_common').window('close');
		break;
	 }
 }
});


var heightRealGlobal = 0;
$(function(){

	keyRandom = $('#keyRandom').val();
	//close the window
	$('#win').dialog('close');
	$('#win_common').dialog('close');
	
	//添加数据
	$('#form_addButton').click(function(){
		formAddSubmit();
	});

	//双击datagrid,弹编辑框
	datagrid.datagridObj.onDblClickRow = function (rowIndex, rowData) {  
		editRow(rowIndex);
	};

	//============resize condition layout===========
	function resizeConditionLayoutHeight(){
		//condition div content height
		var heightReal = $("#_conditionContentDiv").height();
		if(heightReal!=0 && heightReal != heightRealGlobal){
			heightRealGlobal = heightReal;
			var height = heightReal+15;
			$('body').layout('panel', 'north').panel('resize',{height:height+"px"});
			$('body').layout('resize');
		}
	}
	//没有查询条件的话，删除掉north区
	var heightReal = $("#_conditionContentDiv").height();
	if(heightReal==0){
		$('body').layout('remove', 'north');
	}else{
		resizeConditionLayoutHeight();
		setInterval(resizeConditionLayoutHeight,500);// 注意函数名没有引号和括弧！
	}

	window.onload = inputPlaceholder();

});


function downloadReport(){
	//document.downloadReportForm.submit();
	var url = 'template_downloadReport.action?_keyRandom='+_keyRandom;
	var exportExcel = "export_excel";
	//dataParams[exportExcel] = 1;
	//var params = $.param(dataParams);
	//var url = host+"&"+params;
	$('<form method="post" action="' + url + '"></form>').appendTo('body').submit().remove();
	//delete dataParams[exportExcel];
};

/*tool bar button on click listen*/
function toolbarBtnOnClick(url,isDialog,width,height,itemName){
	//上个页面通过右击列表 传过来的 id和row; ps:右击模板x列，看模板列列表，自动加载列数，要传模板id找表名
	//var prePageListRow = '${paramValues.row[0]}';
	//prePageListRow = decodeURI(prePageListRow);
    var _id = _prePageListId;
    /*
    if(_prePageListRow!=''){
    	_id = jQuery.parseJSON(_prePageListRow).id;
    }
    */
    
    //============
	var rows = $('#'+datagrid.datagridId).datagrid('getSelections');
	
	//row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
	var rowIdsArr = new Array();
	$.each(rows,function(index,row){
		var rowIds = {'id':row.id};
		rowIdsArr.push(rowIds);
	});
	
	//
	var rowIdsArr = JSON.stringify(rowIdsArr);

	rowIdsArr = encodeURI(rowIdsArr);
	rowIdsArr = encodeURI(rowIdsArr);

	if(url.indexOf('?')!=-1){
		url = url+'&rowIdsArr='+rowIdsArr+'&_id='+_id;
	}else{
		url = url+'?rowIdsArr='+rowIdsArr+'&_id='+_id;
	}
	
    //
	if(isDialog == '1'){
		//url = _path+'/'+url+'?_id='+_id;
		url = _path+'/'+url;
		//居中显示
	    var iframeWidth = $(this).width();
    	var iframeHeight = $(this).height();
	    var setWidth = (iframeWidth - width) / 2;
	    var setHeight = (iframeHeight - height) / 2;

		$.iframe.win({
			url:url,
			title:itemName,
			width:width,
			height:height,
			modal:false,
			left:setWidth,
			top:setHeight
		});

	}else{
		//上个页面通过右击列表 传过来的 id和row; ps:右击模板x列，看模板列列表，自动加载列数，要传模板id找表名
		//url = url +'?_id='+_id;
		window.parent.Open(itemName,url);
	}
};


/*
right menu button on click listen
href：只会引入body部分内容 
*/
function rightMenuItemClick(url,isDialog,width,height,itemName){
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	//row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
	//row.chartJson='';
	$.each(row,function(key,value){
		try 
		{ 
			if(value != '' && isNaN(value)){
				if(value.indexOf("'")!=-1){
					row[key] = '';
				}
				else if(value.indexOf("\"")!=-1){
					row[key] = '';
				}
				else if(value.indexOf("\{")!=-1){
					row[key] = '';
				}
			} 
		} 
		catch (er) 
		{ 
            window.parent.error(er);
		} 
		
	})
	
	//
	var rowtext = JSON.stringify(row);
	rowtext = encodeURI(rowtext);
	rowtext = encodeURI(rowtext);
	
	if(url.indexOf('?')!=-1){
		url = url+'&row='+rowtext+'&_id='+row.id;
		//url = url+'&row=999&_id='+row.id;
	}else{
		url = url+'?row='+rowtext+'&_id='+row.id;
		//url = url+'?row=888&_id='+row.id;
	}
	if(isDialog == '1'){
		url = _path+'/'+url;
		//居中显示
	    var iframeWidth = $(this).width();
    	var iframeHeight = $(this).height();
	    var setWidth = (iframeWidth - width) / 2;
	    var setHeight = (iframeHeight - height) / 2;

		 $.iframe.win({
			 url:url,
			 title:itemName,
			 width:width,
			 height:height,
			 modal:true,
			 left:setWidth,
			 top:setHeight
		 });


	}else{
		//alert(url)
		//window.parent.Open(itemName+id,url+'?row='+row);
		window.parent.Open(itemName+row.id,url);
	}
}
function rightMenuItemClickV2(url,isDialog,width,height,itemName,submitText,callback){

	var tab_title=window.parent.getSelectedTabTitle();
	var jsonParam = {};
	jsonParam.isDialog = isDialog;
	jsonParam.url = _path+url;
	jsonParam.title = tab_title+"-"+itemName+"-"+rowIndex;
	jsonParam.width = width;
	jsonParam.height = height;
	//jsonParam.dialog_style = _style4AddDialog;
	jsonParam.buttons=[];
	jsonParam.buttons[jsonParam.buttons.length]={
		text:submitText,
		iconCls:'icon-save',
		handler:callback
	}
	jsonParam.buttons[jsonParam.buttons.length]={
		text:'关闭',
		iconCls:'icon-cancel',
		handler:function(){
			$.iframe.close();
		}
	}

	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	row = JSON.stringify(row);
	jsonParam.parameter1_iframedialog=row;
	jsonParam.parameter2_iframedialog="344";
	jsonParam.parameter3_iframedialog="565";
	openUrl(jsonParam);
}


function lineTailBtnOnClick(url,isDialog,width,height,itemName,rowIndex){
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	//row 中含有json子对象或双引号，传给下一页parse会出错 ，所以这里就不传了
	//row.chartJson='';
	$.each(row,function(key,value){
		try 
		{ 
			if(value != '' && isNaN(value)){
				if(value.indexOf("'")!=-1){
					row[key] = '';
				}
				else if(value.indexOf("\"")!=-1){
					row[key] = '';
				}
				else if(value.indexOf("\{")!=-1){
					row[key] = '';
				}
			} 
		} 
		catch (er) 
		{ 
            window.parent.error(er);
		} 
		
	})
	
	var rowtext = JSON.stringify(row);
	rowtext = encodeURI(rowtext);
	rowtext = encodeURI(rowtext);
	
	if(url.indexOf('?')!=-1){
		url = url+'&row='+rowtext+'&_id='+row.id;
	}else{
		url = url+'?row='+rowtext+'&_id='+row.id;
	}
	if(isDialog == '1'){
		url = _path+'/'+url;
		//居中显示
	    var iframeWidth = $(this).width();
    	var iframeHeight = $(this).height();
	    var setWidth = (iframeWidth - width) / 2;
	    var setHeight = (iframeHeight - height) / 2;
		$.iframe.win({
			url:url,
			title:itemName,
			width:width,
			height:height,
			modal:false,
			left:setWidth,
			top:setHeight
		});

	}else{
		window.parent.Open(itemName+row.id,url);
	}
}

function removeRow(rowIndex){
	$.messager.confirm('Confirm','确定删除这条记录吗？',function(r){
		if (r){
			var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
			var id = row[_primaryKey];
			var paramData = {};
			paramData[_primaryKey]=id;
			//remove ajax
			$.ajax({
				type:"POST",
				url:_path+_formRemoveAction,
				dataType:"json",
				data:paramData,
				success:function(msg){
					if(JSON.stringify(msg).indexOf('error:')==0){
						window.parent.error(JSON.stringify(msg));
					}else{
						$.messager.alert("操作结果",msg[0].responseContent);
						//成功
						if(msg[0].responseCode==1){
							datagrid_fresh();
						}
					}
				},
				error:function(er){
					window.parent.error(er);
				}
			});
		}
	});
}


function toolbar_add(){
	var tab_title=window.parent.getSelectedTabTitle();
	var jsonParam = {};
	jsonParam.isDialog = _isdialog4AddDialog;
	jsonParam.url = _path+_formAddEditUrl;
	jsonParam.title = tab_title+"-添加框";
	jsonParam.width = 800;
	jsonParam.height = 400;
	jsonParam.buttons=[];
	//以前的信息，不给修改了，去掉保存button
	var selectDate = $("#workPlanDate").textbox("getValue");
	var _2daysLater = GetDateStr(2);
	var result = GetDateDiff(selectDate, _2daysLater, "day");
	result = 0;
	//alert(result);
	if(result>0){//指定日期两天后之前
		jsonParam.buttons[jsonParam.buttons.length]={
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$.iframe.close();
			}
		}
	}else{
		jsonParam.buttons[jsonParam.buttons.length]={
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				formAddSubmit();
			}
		}
		jsonParam.buttons[jsonParam.buttons.length]={
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$.iframe.close();
			}
		}
	}
	jsonParam.dialog_style = _style4AddDialog;

	//自动选中上页传过来的id, 作为外键
	var _foreignKeyJo = {};
	_foreignKeyJo[_foreignKey] = _prePageListId;
	var paramJson = JSON.stringify(_foreignKeyJo);
	/*paramJson = decodeURI(paramJson);
	paramJson = decodeURI(paramJson);
	paramJson = decodeURI(paramJson);*/

	jsonParam.parameter1_iframedialog=paramJson;
	jsonParam.parameter2_iframedialog="23";
	jsonParam.parameter3_iframedialog="56";
	openUrl(jsonParam);
}

//修改数据
function editRow(rowIndex){
	var requestUrl = _formAddAction;
	authRequestUrl(requestUrl,function(authResult){
		editRowInner(rowIndex,authResult);
	});
}


/**
* @param rowIndex
* @param authResult: when true then hidden save button
*/
function editRowInner(rowIndex,authResult){
	var tab_title=window.parent.getSelectedTabTitle();
	var jsonParam = {};
	jsonParam.isDialog = _isdialog4AddDialog;
	jsonParam.url = _path+_formAddEditUrl;
	jsonParam.title = tab_title+"-编辑框-"+rowIndex;
	jsonParam.width = 800;
	jsonParam.height = 400;
	jsonParam.dialog_style = _style4AddDialog;
	jsonParam.buttons=[];

	//以前的信息，不给修改了，去掉保存button
	var selectDate = $("#workPlanDate").textbox("getValue");
	var _2daysLater = GetDateStr(2);
	var result = GetDateDiff(selectDate, _2daysLater, "day");
	//alert(result);
	if(result>0){//指定日期两天后之前
		jsonParam.buttons[jsonParam.buttons.length]={
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$.iframe.close();
			}
		}
	}else{
		//是否有权限访问
		if(authResult){
			jsonParam.buttons[jsonParam.buttons.length]={
				text:'保存',
				iconCls:'icon-save',
				handler:function(){
					formAddSubmit();
				}
			}
		}
		jsonParam.buttons[jsonParam.buttons.length]={
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$.iframe.close();
			}
		}
	}


	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	row = JSON.stringify(row);
	jsonParam.parameter1_iframedialog=row;
	jsonParam.parameter2_iframedialog="344";
	jsonParam.parameter3_iframedialog="565";
	openUrl(jsonParam);
}

/**
 * 调用iframe中的formAddSubmit()
 * 增加form表单提交
 */
function formAddSubmit(){
	window.frames['iframe_dialog'].formAddSubmit();
}


function closeWinCommon(){
	$.iframe.close();
}

/*

//var str = JSON.stringify(row);  
//修改数据
//1，添加和编辑共用form表单，会执行 $("#form_add").form("clear"); 语句，进行表单清空操作，所以要通过js load数据的方式进行
function editRow_removed(rowIndex){

	//居中显示
    var iframeWidth = $(this).width();
	var iframeHeight = $(this).height();
    var setWidth = (iframeWidth - _width4AddDialog) / 2;
    var setHeight = (iframeHeight - _height4AddDialog) / 2;
	
    //用这个打开dialog可以拖动
	$('#win').dialog({
		title:"编辑数据框", 
	    modal:true, 
	    left:setWidth,
	    top:setHeight });//动态修改window title
	//$('#win').dialog('open');
	
	//clear form
	$("#form_add").form("clear"); 
	//load keyrandom
	//$('#form_add').form('load',{'keyRandom':keyRandom});
	//load form data
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	$('#form_add').form('load',row);	

}


var keyRandom;
//Add button from toolbar
function toolbar_add_removed(){
	//居中显示
	var iframeWidth = $(this).width();
	var iframeHeight = $(this).height();
	var setWidth = (iframeWidth - _width4AddDialog) / 2;
	var setHeight = (iframeHeight - _height4AddDialog) / 2;

	//用这个打开dialog可以拖动
	$('#win').dialog({
		title:"添加数据框",
		modal:true,
		left:setWidth,
		top:setHeight });
	//$('#win').dialog('open');

	//clear form
	$("#form_add").form("clear");
	//load keyrandom
	//$('#form_add').form('load',{'keyRandom':keyRandom});
	//使用上一页面传过来的ID，作为查询条件，PS:查询一个公司下所有部门，在公司列表中右击查询，传_id，作为部门的外建
	//load foreign key  s:if test="#parameters._id!=null && #column.propertyName==#attr.templateTable.foreignKey"
	//alert(_foreignKey+":"+_prePageListId);
	var _foreignKeyJo = {};
	_foreignKeyJo[_foreignKey] = _prePageListId;
	$('#form_add').form('load',_foreignKeyJo);
	//disable foreign key combobox, add a hidden input dynamic for submit form
	$('#'+_foreignKey+"4Add").combobox('disable');
	$("<input id='_foreignKeyHidden' name='"+_foreignKey+"' type='text' size=10 value=''/>").appendTo($('#'+_foreignKey+"4Add"));
	$('#_foreignKeyHidden').val(_prePageListId);

	//加载默认值
	//var jsonstr = jQuery.parseJSON($('#_defaultValueJOStr').val());
	$('#form_add').form('load', _defaultValueJOStr);

}
*/
